import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入登录组件
import Login from '@/views/Login.vue'
// 导入主页组件
import Home from '@/views/Home.vue'
// 导入欢迎组件
import Welcome from '@/views/Welcome.vue'
// 导入用户组件
import Users from '@/views/user/Users.vue'
// 导入权限列表组件
import Rights from '@/views/power/Rights.vue'
// 导入角色列表组件
import Roles from '@/views/power/Roles.vue'
// 导入商品分类组件
import Category from '@/views/goods/Category.vue'
// 导入分类参数组件
import Params from '@/views/goods/Params.vue'
// 导入商品列表组件
import Goodslist from '@/views/goods/Goodslist.vue'
// 导入添加商品组件
import AddGood from '@/views/goods/AddGood.vue'
// 导入订单列表组件
import OrderList from '@/views/order/OrderList.vue'
// 导入数据报表组件
import Reports from '@/views/report/Reports.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Category },
      { path: '/params', component: Params },
      { path: '/goods', component: Goodslist },
      { path: '/goods/addgood', component: AddGood },
      { path: '/orders', component: OrderList },
      { path: '/reports', component: Reports }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由守卫,监控是否存在token
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const myToken = window.sessionStorage.getItem('token')
  if (!myToken) return next('/login')
  next()
})
export default router
